package gdma.hqdes.stat.bean;

import com.deepoove.poi.data.ChartMultiSeriesRenderData;
import com.deepoove.poi.data.SeriesRenderData;
import lombok.Data;

import java.util.*;
import java.util.function.Function;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2025-01-08
 */

@Data
public class ChartMultiSeriesRenderDataEx extends ChartMultiSeriesRenderData {

    String comment;
    Map<String, List<Object>> categoryMetas;
    List<Number> seriesAverages;

    public void addCategoryMeta(String metaName, List<Object> metaData) {
        if(categoryMetas == null) categoryMetas = new HashMap<>();
        categoryMetas.put(metaName, metaData);
    }

    public void addCategoryMetas(String key, Function<String, Object> metaFunc) {
        List<Object> metadatas = new ArrayList<>();
        for(String category : getCategories()) {
            Object metadata = metaFunc.apply(category);
            metadatas.add(metadata);
        }
        addCategoryMeta(key, metadatas);
    }

    public void calcSeriesAverages() {
        List<Number> seriesAvgs = new ArrayList<>();
        for(SeriesRenderData series: getSeriesDatas()) {
            double average = Arrays.stream(series.getValues())
                    .mapToDouble(Number::doubleValue)
                    .average().orElse(0);
            seriesAvgs.add(average);
        }
        this.seriesAverages = seriesAvgs;
    }

}
